package CelestialWeb;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import CelestialWorkflow.*;
import CelestialDataAccess.*;

public class ServletCustomerLogin extends HttpServlet {
   
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String strEmail = request.getParameter("txtEmail");
        String strPassword = request.getParameter("txtPassword");
        String strRememberMe  = request.getParameter("chkRememberMe");

        /* save email in cookie if customer checked "Remember me" */
        if(strRememberMe.compareToIgnoreCase("true")==0){
            Cookie ckeRememberMe = new Cookie("RememberMe", strEmail);
            ckeRememberMe.setMaxAge(7*24*60*60);
            response.addCookie(ckeRememberMe);
        }
        else{
            Cookie ckeRememberMe = new Cookie("RememberMe", "");
            ckeRememberMe.setMaxAge(0);
            response.addCookie(ckeRememberMe);
        }

        LoginControl lgctrl = new LoginControl();
        Customer cust = lgctrl.validateLogin(strEmail, strPassword);
        if(cust!=null){

            /* customer is authenticated, add cookie to track session */
            Cookie ckeUser = new Cookie("FirstName", cust.getFirstName());
            Cookie ckeUserLevel = new Cookie("UserLevel", Integer.toString(cust.getUserLevel()));

            response.addCookie(ckeUser);
            response.addCookie(ckeUserLevel);
        }
        else{
            /* authentication fails, see what's the error */

            response.setContentType("text/xml");
            response.setHeader("Cache-control", "no-cache");
            PrintWriter pw = response.getWriter();

            assert lgctrl.getError() > 0;
            switch(lgctrl.getError()){
                case LoginControl.NO_EMAIL:
                    pw.println("<Error1>Oop! You missed out the Email</Error1>");
                    break;
                case LoginControl.NO_PASSWORD:
                    pw.println("<Error1>Oop! You missed out the Password</Error1>");
                    break;
                case LoginControl.WRONG_PASSWORD:
                    pw.println("<Error1>The Email and Password do not match. Did you forget your password?</Error1>");
                    break;
                case LoginControl.INVALID_EMAIL:
                    pw.println("<Error1>This Email does not seems to be valid. Have you mistyped?</Error1>");
                    break;
                case LoginControl.EMAIL_NOT_FOUND:
                    pw.println("<Error1>This Email does not have a record in our database. Are you trying to register an account with it?</Error1>");
                    break;
                case LoginControl.SYSTEM_ERROR:
                    pw.println("<Error1>System Error: " + lgctrl.getErrorMessage() + "</Error1>");
                    break;
            }
        }
    } 
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
